package fatworm.query;

import java.util.Collection;
import java.util.HashMap;

import fatworm.query.constant.Constant;
import fatworm.query.expression.Expression;
import fatworm.util.*;

public class ProjectScan implements Scan {

	private Scan s;
	private HashMap<String, Expression> fieldlist = new HashMap<String, Expression>();
	
	public ProjectScan(Scan s, Collection<Expression> selectlist) {
		this.s = s;
		
		for (Expression e : selectlist)
			fieldlist.put(e.toString(), e);
	}
	public void beforeFirst() {
		s.beforeFirst();
	}
	
	public boolean next() {
		return s.next();
	}
	
	public void close() {
		s.close();
	}
	
	public Constant getVal(String name) {
		if (fieldlist.keySet().contains(name))
			return fieldlist.get(name).evaluate(s);
		
		Lib.assertNotReached();
		return null;
	}
	
/*	public int getInt(String name) {
		if (fieldlist.contains(name))
			return s.getInt(name);
		
		Lib.assertNotReached();
		return 0;
	}
	
	public String getString(String name) {
		if (fieldlist.contains(name))
			return s.getString(name);
		
		Lib.assertNotReached();
		return null;
	}*/
	
	public boolean hasField(String name) {
		return fieldlist.keySet().contains(name);
	}
}